package com.example.m2l_digicode;

import java.sql.DriverManager;
import java.sql.SQLException;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.ResultSet;

public class Login extends Activity {

    static ResultSet result;
    static PreparedStatement statement;
    static Handler handler = null;

    static String login_saisi=null;
    static String mdp_saisi=null;
    static String login_bdd=null;
    static String mdp_bdd=null;

    static AlertDialog login_erreur;
    static AlertDialog mdp_erreur;
   
    ProgressDialog loadingdialog;
    
    java.sql.Connection conn = null;
    public final static String EXTRA_MESSAGE = "com.example.m2l_digicode.MESSAGE";
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		// Show the Up button in the action bar.
		getActionBar().setDisplayHomeAsUpEnabled(true);
		
       	
		
        final Button button_connect = (Button) findViewById(R.id.bt_connect);
        button_connect.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	
            	
            	EditText champ_mail = (EditText) findViewById(R.id.login);
            	EditText champ_mdp = (EditText) findViewById(R.id.mdp);
            	
            	login_saisi=champ_mail.getText().toString();
            	mdp_saisi=champ_mdp.getText().toString();
            	
            	verif_login();

            }  
        });
        
		
	}
	
	
	public void verif_login()
	{
	    
		new Thread(new Runnable() {
	        @Override
	        public void run() {
	        	String requete = "SELECT ADRESSE_MAIL, MDP FROM demandeur WHERE ADRESSE_MAIL='"+login_saisi+"'";
	        	
                runOnUiThread(new Runnable() {	                   
					public void run() {
						loadingdialog = ProgressDialog.show(Login.this,"","Connexion en cours...",true);
                   }
               });
                
			
		       try
		       {
		           
		           try {
		               Class.forName("com.mysql.jdbc.Driver").newInstance();
		        
		           } catch (InstantiationException e) {
		               // TODO Auto-generated catch block
		               e.printStackTrace();
		               Log.e("Instanciation Driver", e.getMessage());
		           } catch (IllegalAccessException e) {
		               // TODO Auto-generated catch block
		               e.printStackTrace();
		               Log.e("Access Exception", e.getMessage());
		           } catch (ClassNotFoundException e) {
		               // TODO Auto-generated catch block
		               e.printStackTrace();
		               Log.e("Class", e.getMessage());
		           }
		                    
		            
		           String url = "jdbc:mysql://192.168.1.26/fredi?autoReconnect=true";
		           //String url = "jdbc:mysql://ppe-fredi.suroot.com/fredi?autoReconnect=true";
		           String user = "util";
		           String passwd = "util123";
		            
		         
		           conn = (java.sql.Connection) DriverManager.getConnection(url, user, passwd);
		               Log.i("Connection", "Etablished");

		               statement=(PreparedStatement) conn.prepareStatement(requete);
		               result=(ResultSet) statement.executeQuery();
		               
		                while(result.next())
		                {
		                	login_bdd=result.getString(1);
		                	mdp_bdd=result.getString(2);
		                }
		                
		                
		                runOnUiThread(new Runnable() {
		                    @SuppressWarnings("deprecation")
							public void run() {
				                
		                    	//Si la requete ne renvoi aucun mot de passe pour l'Email saisi
				                if(mdp_bdd==null)
				                {
				                
					                runOnUiThread(new Runnable() {
					                   
										public void run() {
     
					                    	login_erreur = new AlertDialog.Builder(Login.this).create();
					                        login_erreur.setTitle("Erreur");
					                        login_erreur.setMessage("Adresse Email incorrecte");       
					                        login_erreur.setButton("OK", new DialogInterface.OnClickListener() {
					                            public void onClick(DialogInterface dialog, int which) {
					                            	login_erreur.dismiss();
					                          } });
					                        login_erreur.show();
				       
					                   }
					               });
				                }
				                else
				                {
				                	if(mdp_saisi.equals(mdp_bdd))
				                	{
				                		Intent intent_demandeur = new Intent(Login.this, EspaceDemandeur.class);
				                		intent_demandeur.putExtra(EXTRA_MESSAGE, login_bdd);
				                		startActivity(intent_demandeur);
				                	}
				                	else
				                	{
				                    	mdp_erreur = new AlertDialog.Builder(Login.this).create();
				                        mdp_erreur.setTitle("Erreur");
				                        mdp_erreur.setMessage("Mot de passe incorrecte");            
				                        mdp_erreur.setButton("OK", new DialogInterface.OnClickListener() {
				                            public void onClick(DialogInterface dialog, int which) {
				                            	mdp_erreur.dismiss();
				                          } });
				                        mdp_erreur.show();
				                	}
				                }
				                			                
				                loadingdialog.dismiss();
		                    }  
			               });
		       }
		       
		       catch(SQLException e)
		       {
		           e.printStackTrace();
		                            
		           Log.e("MySQL", e.getMessage());
		           
		           loadingdialog.dismiss();
		         
		       }	  
	        }
	    }).start();
	    	    
	}

}
